package com.njtoyo.taxi.admin.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.njtoyo.taxi.admin.cache.CacheFinder;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.DataAuthSqlWhere;
import com.njtoyo.taxi.admin.rest.wrapper.Identity;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.njtoyo.taxi.entity.backend.Enum;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author mdf
 * @title: AuthUtil
 * @description: TODO
 * @date 2024/3/28 10:20
 */
public class AuthUtil {


    public static DataAuthSqlWhere getDataAuthSqlWhere() {
        DataAuthSqlWhere sqlWhere = new DataAuthSqlWhere();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Identity identity = (Identity) request.getAttribute("identity");
        if (ObjectUtil.isNull(identity)) {
            throw new RuntimeException("身份信息为空");
        }
        AdminUser adminUser = identity.getAdminUser();
        AdminUserIdentityPresenter identityPresenter = (AdminUserIdentityPresenter) identity.getAdminUser().getIdentity();
        if (!adminUser.getAccountType().equals(Enum.AccountType.platform)) {
            switch (adminUser.getAccountType()) {
                case company:
                case canton:
                    sqlWhere.setCantonIds(identityPresenter.getCantonIds());
                    sqlWhere.setAreaIds(identityPresenter.getAreaIds());
                    break;
            }
        }
        return sqlWhere;
    }


    public static AdminUser getUserInfo() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Identity identity = (Identity) request.getAttribute("identity");
        if (ObjectUtil.isNull(identity)) {
            throw new RuntimeException("身份信息为空");
        }
        AdminUser adminUser = identity.getAdminUser();
        return adminUser;
    }
}
